package com.rongshu.sendmessage.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.Nullable;

import com.rongshu.sendmessage.R;


/**
 * 标题布局
 */

public class TitileLayout extends LinearLayout implements View.OnClickListener {
    private Context context;
    private View container;
    private LinearLayout layTitle;
    private LinearLayout titleLayLeft;
    private ImageView titleIvLeft;
    private TextView titleTvLeft;
    private TextView title;
    private LinearLayout titleLayRight;
    private ImageView titleImgRight;
    private TextView titleTvRight;
    private TitleClickListener titleClickListener;
    private String titleStr = "";
    private boolean backShow = false;

    public TitileLayout(Context context) {
        super(context);
        this.context = context;
    }

    public TitileLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        getAttr(attrs);
    }

    public TitileLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        getAttr(attrs);
    }

    private void getAttr(AttributeSet attrs) {
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TitleAttr);
        titleStr = ta.getString(R.styleable.TitleAttr_titleStr);
        backShow = ta.getBoolean(R.styleable.TitleAttr_backShow, false);
        ta.recycle();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        container = LayoutInflater.from(context).inflate(R.layout.common_title, null);
        layTitle =  container.findViewById(R.id.layTitle);
        titleLayLeft = container.findViewById(R.id.titleLayLeft);
        titleIvLeft =  container.findViewById(R.id.titleIvLeft);
        titleTvLeft = container.findViewById(R.id.titleTvLeft);
        title =  container.findViewById(R.id.title);
        titleLayRight = container.findViewById(R.id.titleLayRight);
        titleImgRight = container.findViewById(R.id.titleImgRight);
        titleTvRight = container.findViewById(R.id.titleTvRight);
        titleLayLeft.setOnClickListener(this);
        titleIvLeft.setOnClickListener(this);
        titleTvLeft.setOnClickListener(this);
        titleLayRight.setOnClickListener(this);
        titleTvRight.setOnClickListener(this);
        titleImgRight.setOnClickListener(this);
        title.setOnClickListener(this);
        if (titleStr!=null) {
            title.setText(titleStr);
        }
        if (backShow) {
            titleLayLeft.setVisibility(VISIBLE);
        } else {
            titleLayLeft.setVisibility(INVISIBLE);
        }
        addView(container);
    }

    public void setTitleClickListener(TitleClickListener titleClickListener) {
        this.titleClickListener = titleClickListener;
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.titleLayLeft || id == R.id.titleIvLeft || id == R.id.titleTvLeft) {
            if (null != titleClickListener) {
                titleClickListener.onLeftClick();
            }
        } else if (id == R.id.title) {
            if (null != titleClickListener) {
                titleClickListener.onTitleClick();
            }
        } else if (id == R.id.titleLayRight || id == R.id.titleImgRight || id == R.id.titleTvRight) {
            if (null != titleClickListener) {
                titleClickListener.onRightClick();
            }
        }
    }
    /**
     * 显示左边按钮
     */
    public void showLeft() {
        titleLayLeft.setVisibility(VISIBLE);
    }

    /**
     * 隐藏左边按钮
     */
    public void hideLeft() {
        titleLayLeft.setVisibility(INVISIBLE);
    }

    /**
     * 显示右边按钮
     */
    public void showRight() {
        titleLayRight.setVisibility(VISIBLE);
    }

    /**
     * 隐藏右边按钮
     */
    public void hideRight() {
        titleLayRight.setVisibility(INVISIBLE);
    }

    /**
     * 设置左侧按钮文本
     *
     * @param text
     */
    public void setLeftText(String text) {
        titleTvLeft.setText(text);
        titleTvLeft.setVisibility(VISIBLE);
        titleLayLeft.setVisibility(VISIBLE);
    }

    /**
     * 设置右侧按钮文本
     *
     * @param text
     */
    public void setRightText(String text) {
        titleTvRight.setText(text);
        titleLayRight.setVisibility(VISIBLE);
        titleImgRight.setVisibility(GONE);
    }

    /**
     * 设置title
     *
     * @param text
     */
    public void setTitle(String text) {
        if(text!=null) {
            title.setText(text);
        }
    }

    /**
     * 获取左侧按钮图片
     *
     * @return
     */
    public ImageView getLeftImg() {
        return titleIvLeft;
    }

    /**
     * 获取右侧按钮图片
     *
     * @return
     */
    public ImageView getRightImg() {
        return titleImgRight;
    }

    /**
     * 设置右侧图标
     * */
    public void setRightImg(int res){
        titleTvRight.setVisibility(INVISIBLE);
        titleImgRight.setImageResource(res);
        titleLayRight.setVisibility(VISIBLE);
    }
    /***
     * 设置左侧图标
     *
     */
    public void setLeftImg(int res){
        titleTvLeft.setVisibility(INVISIBLE);
        titleIvLeft.setImageResource(res);
        titleLayLeft.setVisibility(VISIBLE);
    }
    /***
     * 设置左侧图标
     *
     */
    public void hideLeftImageShowText(String text){
        titleTvLeft.setVisibility(VISIBLE);
        titleIvLeft.setVisibility(GONE);
        titleTvLeft.setText(text);
        titleLayLeft.setVisibility(VISIBLE);
    }

    public void TitleBackground(int text_white) {
        layTitle.setBackgroundResource(text_white);
    }

    public  interface TitleClickListener {
         void onLeftClick();

        void onRightClick();

        void onTitleClick();
    }
    public abstract static class TitleClickListenerA implements TitleClickListener{
        public void onLeftClick(){}
        public void onRightClick(){}
        public void onTitleClick(){}
    }
}
